<?php
/**
 * Copy Right IJH.CC
 * $Id$
 */

class Mdl_Helper_Debug extends Model
{
    
    protected $__DU = array('__DEBUG__'=> 'test');
    
    public function set($k,$v)
    {
        $this->__DU[$k] = $v;
    }
    
    /**
     * $level 调试级别{data:仅调试数据,sys系统}
     */
    public function output($level='data', $return=false)
    {
        if(!$level){
            return false;
        }
        if($return){
            $content = ob_get_contents();
            ob_end_clean();
            ob_start();
        }
        //以后添加调试转出模板
        echo '<div id="__SYSTEMDEBUG__" style="background-color:#FFFFFF;border:2px dashed #FF3300;padding:5px;overflow-x:auto;overflow-y:scroll;height:400px;display:none;position: fixed;right:0px;bottom:10px;z-index:100;">';
        echo "<h4>江湖信息科技系统框架调试信息</h4><hr />\n";
        echo "<pre>\n+------------------------------------------------------------------------+\n";
        //echo '系统级运行时间:'.xdebug_time_index()."秒\n";
        echo '框架级运行时间:'.$this->runtime()."秒\n";
        //echo 'Smarty处理时间:'.K::$system->smarty_execute_time."秒\n";
        echo 'Smarty处理时间:'.(microtime(true) - K::$system->smarty_start_time)."秒\n";
        echo '内存使用情况:'.K::M('helper/format')->size(memory_get_usage())."\n";
        echo '内存使用峰值:'.K::M('helper/format')->size(memory_get_peak_usage())."\n";
        echo "\n+------------------------------------------------------------------------+\n";
        echo "用户调试信息:\n";
        if($this->__DU){
            foreach((array)$this->__DU as $k=>$v){
                echo "$k:\n";
                if(is_array($v)){
                    print_r($v);
                }else if(is_string($v)){
                    echo "$v\n";
                }else{
                    var_dump($v);
                }
            }
        }
        if($level == 'system'){
            echo "\n+------------------------------------------------------------------------+\n";
            $QSQL = $this->db->SQLLOG();
            $c = count($QSQL['QSQL']);
            echo "系统执行的SQL语句:共({$c})个\n";
            //var_dump($this->db->_QSQL);
            echo "SQL执行时间:{$QSQL['Time']}秒\n";
            print_r($QSQL);
            echo "\n+------------------------------------------------------------------------+\n";
            $c = count(Import::$_GFILES);
            echo "系统加载的MDL,CTL文件:共({$c})个\n";
			foreach(Import::$_GFILES as $k=>$v){
				echo "{$k}:{$v}\n";
			}
            echo "\n+------------------------------------------------------------------------+\n";
            $FS = get_included_files();
            $c = count($FS);
            echo "系统所有引入的文件:共({$c})个\n";
			foreach($FS as $k=>$v){
				echo "{$k}:{$v}\n";
			}
            echo "\n+------------------------------------------------------------------------+\n";
            echo "系统运行信息:\n";
            //var_dump(self::$system);
        }
        echo "\n+------------------------------------------------------------------------+\n";
        echo '</pre></div><a id="__SYSTEMDEBUG_BUTTON__" style="width:100px;height:36px;display:block;position: fixed;right:0px;bottom:10px;z-index:1000;background-image: url(\'\');"></a>';
echo '
<script type="text/javascript">
(function(K, $){
$("#__SYSTEMDEBUG_BUTTON__").toggle(function(){$("#__SYSTEMDEBUG__").show();},function(){$("#__SYSTEMDEBUG__").hide();});
$("#__SYSTEMDEBUG__").width($(window).width()-20);
})(window.KT, window.jQuery);
</script>';
        if($return){
            $info = ob_get_contents();
            ob_end_clean();
            echo $content;
            return $info;
        }
        return true;
    }
    
    //返回系统运行时间
    public function runtime()
    {
        return microtime(true) - self::$system->starttime;
    }
}